package com.hmall.order.amqp;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

import static com.hmall.order.amqp.MqConstants.*;

/**
 * 创建队列，交换机并绑定
 *
 * @Author 15198
 * @Date 2022/8/13 16:39
 * @Description: 创建队列，交换机并绑定
 * @Created by
 */
@Configuration
public class AmqpQueueAndExchange {

    //死信交换机
    @Bean
    public DirectExchange orderDeadExchange() {
        return new DirectExchange(ORDER_DEAD_EXCHANGE, true, false);
    }


    //死信队列
    @Bean
    public Queue orderDeadQueue() {
        return new Queue(MqConstants.ORDER_DEAD_QUEUE, true, false, false);
    }

    //绑定
    @Bean
    public Binding orderDeadBinding(){
        return BindingBuilder.bind(orderDeadQueue()).to(orderDeadExchange()).with(ORDER_DEAD_ROUTINGKEY);
    }


    //正常交换机
    @Bean
    public DirectExchange orderExchange(){
        return new DirectExchange(ORDER_EXCHANGE, true, false);
    }

    //正常队列
    @Bean
    public Queue orderInfo() {
        Map<String, Object> rule = new HashMap<>();
        //正常队列中消息延迟3分钟
        rule.put("x-message-ttl", 1000*60*3);
        rule.put("x-dead-letter-exchange",ORDER_DEAD_EXCHANGE);
        rule.put("x-dead-letter-routing-key",ORDER_DEAD_ROUTINGKEY);
        return new Queue(ORDER_QUEUE, true, false, false, rule);
    }


        //绑定正常交换机和正常队列
    @Bean
    public Binding orderBinding() {
        return BindingBuilder.bind(orderInfo()).to(orderExchange()).with(ORDER_ROUTINGKEY);
    }




}
